const db = wx.cloud.database();
const innerAudioContext = wx.createInnerAudioContext();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    showCollect:null,
    word:null
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {

    //点击播放录音
    gotoPlay: function (e) {
      var filePath = e.currentTarget.dataset.key;

      if (null == filePath || "" == filePath) {
        filePath = this.data.wordResult.audio;
      }
      innerAudioContext.src = filePath;
      console.info(filePath);
      innerAudioContext.play();
    },
    //收藏该单词
    collectWord: function (e) 
    {
      let word = this.properties.word;
      try {
        let myWords = wx.getStorageSync('my_word');
        if (!myWords) {
          myWords = new Array();
        }
        if (this.array_contain(myWords, word))
        {
          wx.showToast({
            icon: "none",
            title: '您已经收藏过该词汇'
          })
        }else
        {
          myWords.push(word);
          wx.showToast({
            icon:"none",
            title: '收藏成功'
          })
        }
        wx.setStorageSync('my_word', myWords);

      } catch (e) {

      }   

    },
    array_contain: function(array, obj){
        for(var i = 0; i<array.length; i++){
          if (array[i]._id == obj._id)
            return true;
        }
        return false;
    }
  }
})
